<script>
    Namespace.register("EBCMS.Form_{$field.unique}");
    $(function() {
        /*改名名称*/
        EBCMS.Form_{$field.unique}.changename = function(id,value){
            $(id).attr('name','{$field.field}['+value+']');
        };
        /*上移*/
        EBCMS.Form_{$field.unique}.up = function(dom){
            var thisdom = $(dom).parent().parent().parent();
            if (thisdom.prev().length) {
                thisdom.insertBefore(thisdom.prev());
            }
        };
        /*下移*/
        EBCMS.Form_{$field.unique}.down = function(dom){
            var thisdom = $(dom).parent().parent().parent();
            if (thisdom.next().length) {
                thisdom.next().insertBefore(thisdom);
            }
        };
        EBCMS.Form_{$field.unique}.render_text = function(name,value,target){
            name = name||'EB_' + EBCMS.FN.random_str(6);
            opt = {
                name:name,
                value:value||'',
                id:'{$field.unique}_' + EBCMS.FN.random_str(15),
            };
            var str = '<tr>';
            str += '<td style="width:180px;">';
            str += '<div class="btn-group">';
            str += '<button type="button" class="btn btn-primary" onclick="$(this).parent().parent().parent().remove();">删除</button>';
            str += '<button type="button" class="btn btn-primary" onclick="EBCMS.Form_{$field.unique}.up(this);">上移</button>';
            str += '<button type="button" class="btn btn-primary" onclick="EBCMS.Form_{$field.unique}.down(this);">下移</button>';
            str += '</div>';
            str += '</td>';
            str += '<td style="width:140px;">';
            str += '<input type="text" class="form-control" value="'+opt.name+'" onKeyUp="EBCMS.Form_{$field.unique}.changename(\'#'+opt.id+'\',$(this).val());" placeholder="填写名称">';
            str += '</td>';
            str += '<td>';
            str += '<input type="text" class="form-control" id="'+opt.id+'" name="{$field.field}['+opt.name+']" value="'+opt.value+'" placeholder="填写值">';
            str += '</td>';
            str += '</tr>';
            if (target) {
                $(target).append(str);
            }else{
                return str;
            }
        };
        
        var forms = {$field.value|json_encode};
        if (typeof forms!='object') {
            forms = {};
        }
        $.each(forms, function(name, val) {
            EBCMS.Form_{$field.unique}.render_text(name,val,'#{$field.unique}_container');
        });
    });
</script>
<tr>
    <th>{$field.title}</th>
    <td>
        <div class="btn-group btn-group-sm">
            <button type="button" class="btn btn-primary" onclick="EBCMS.Form_{$field.unique}.render_text('','','#{$field.unique}_container');">添加</button>
        </div>
        {notempty name='field.remark'}
            <p class="help-block">{$field.remark}</p>
        {/notempty}
    </td>
</tr>
<tr>
    <th></th>
    <td>
        <div style="border: 1px solid #ddd;">
            <table class="table table-noborder" id="{$field.unique}_container"></table>
        </div>
    </td>
</tr>